package org.andao.commons.lucene.core.extractor;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.andao.commons.lucene.bean.LuceneResult;
import org.andao.commons.lucene.core.mapper.IndexMapper;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;



/**
 * lucene搜索结果抽离器
 * 
 * @author Seewo Software - Vick.liu (liuguanglin@cvte.cn)
 * 
 */
public class SolrDocumentListExtractor implements DocumentListExtractor {
	/**
	 * 索引映射器，index2Bean
	 */
	private IndexMapper indexMapper;
	/**
	 * 例外的行
	 */
	private int rowsExpected;

	// Constructors start
	public SolrDocumentListExtractor(IndexMapper mapper) {
		this(mapper, 0);
	}

	public SolrDocumentListExtractor(IndexMapper indexMapper, int rowsExpected) {
		super();
		this.indexMapper = indexMapper;
		this.rowsExpected = rowsExpected;
	}

	// Constructors end

	/**
	 * 抽离（解析）数据，index2Bean
	 */
	@Override
	public Object extractData(SolrDocumentList list, Map<String, Map<String, List<String>>> highlightMap) {
		List<Object> results = (this.rowsExpected > 0 ? new ArrayList<Object>(this.rowsExpected) : new ArrayList<Object>());
		int rowNum = 0;
		Iterator<SolrDocument> it = list.iterator();
		while (it.hasNext()) {
			results.add(this.indexMapper.mapIndex(new LuceneResult(it.next(), highlightMap), rowNum++));
		}
		return results;
	}

}
